package easyway.core.dao;

import java.io.IOException;
import java.io.Reader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;

import easyway.core.exceptions.ApplicationException;

/**
 *
 * @author zhangqihang
 * @date Nor 23, 2009
 */
public class DaoConfig {
	
	private static final Log log = LogFactory.getLog(DaoConfig.class);
	
	private static final String RESOURCE = "easyway/core/sqlmap/dao.xml";
	
	private static DaoManager daoManager;
	
	static {
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(RESOURCE);
			daoManager = DaoManagerBuilder.buildDaoManager(reader);
		} catch (IOException e) {
			e.printStackTrace();
			log.error("DaoConfig读取初始化ibatis文件失败");
			throw new ApplicationException(e).asSystemException();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
				log.error("DaoConfig流通道关闭异常");
				throw new ApplicationException(e).asSystemException();
			}
		}
	}
	
	/**
	 * 获取DaoManager实例
	 * 
	 * @return DaoManager实例
	 */
	public static DaoManager getDaoManager() {
		return daoManager;
	}

}
